package campus.rest;

import me.zhengjie.aop.log.Log;
import campus.domain.CampusTransferForm;
import campus.service.CampusTransferFormService;
import campus.service.dto.CampusTransferFormQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author 李诗莹
* @date 2020-03-13
*/
@Api(tags = "转账表管理")
@RestController
@RequestMapping("/api/campusTransferForm")
public class CampusTransferFormController {

    private final CampusTransferFormService campusTransferFormService;

    public CampusTransferFormController(CampusTransferFormService campusTransferFormService) {
        this.campusTransferFormService = campusTransferFormService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('campusTransferForm:list')")
    public void download(HttpServletResponse response, CampusTransferFormQueryCriteria criteria) throws IOException {
        campusTransferFormService.download(campusTransferFormService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询转账表")
    @ApiOperation("查询转账表")
    @PreAuthorize("@el.check('campusTransferForm:list')")
    public ResponseEntity<Object> getCampusTransferForms(CampusTransferFormQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(campusTransferFormService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增转账表")
    @ApiOperation("新增转账表")
    @PreAuthorize("@el.check('campusTransferForm:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody CampusTransferForm resources){
        return new ResponseEntity<>(campusTransferFormService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改转账表")
    @ApiOperation("修改转账表")
    @PreAuthorize("@el.check('campusTransferForm:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody CampusTransferForm resources){
        campusTransferFormService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除转账表")
    @ApiOperation("删除转账表")
    @PreAuthorize("@el.check('campusTransferForm:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        campusTransferFormService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}